import BaseItemView from "../../cfw/mvc/BaseItemView";
import ModuleManager from "../../cfw/module/ModuleManager";
import { ResType } from "../../cfw/res/ResInterface";
import ResItem from "../../cfw/res/ResItem";
import BaseItemModel from "../../cfw/mvc/BaseItemModel";
import { ItemID } from "../../public/Config";
import StringHelper from "../../cfw/tools/StringHelper";
import ItemController from "./ItemController";
import { ItemState } from "../../cfw/tools/Define";
import CCButtonEx from "../../public/comps/CCButtonEx";
import BagManager from "../bag/BagManager";


const { ccclass, property } = cc._decorator;

@ccclass
export default class ItemView extends BaseItemView {


    @property(cc.Sprite)
    icon: cc.Sprite = null;

    @property(cc.Sprite)
    ad: cc.Sprite = null;

    @property(cc.Sprite)
    coin: cc.Sprite = null;

    @property(cc.Label)
    coinLabel: cc.Label = null;


    @property(cc.Sprite)
    lock: cc.Sprite = null;


    setTouchEnable(flag: boolean) {
        let button = this.icon.getComponent(CCButtonEx)
        if (button) {
            button.setTouchEnable(flag)
        }
    }

    start() {
        super.start()
    }

    // onDestroy() {
    //     super.onDestroy();
    //     this.model.off(BaseItemModel.UPDATE_STATE, this.updateState, this)
    // }

    updateState() {
        if (!cc.isValid(this.node) || !this.model) {
            return;
        }     
        let cost: number[] = this.model.getCostItem()
        if (cost && cost.length >= 2 && this.model.getState() == ItemState.NOT_GET) {

            switch (cost[0]) {
                case ItemID.AD:
                    this.coin.node.active = false
                    this.ad.node.active = true;
                    this.coinLabel.node.active = false;
                    // this.ad.node.scale = this.model.getScale()
                    break;
                case ItemID.COIN:
                    this.ad.node.active = false;
                    this.coin.node.active = true;
                    this.coinLabel.node.active = true;
                    // this.coin.node.scale = this.model.getScale()
                    this.coinLabel.string = StringHelper.getShortStr(cost[1])
                    break;
            }
        } else {
            this.ad.node.active = false;
            this.coin.node.active = false
            this.coinLabel.node.active = false;
        }
        if (this.model.isOpen()) {
            this.lock.node.active = false
        } else {
            this.lock.node.active = true;
        }
    }

    protected model: BaseItemModel
    protected controller: ItemController;

    addListener(){
        this.eventProxy.on(BaseItemModel.UPDATE_STATE, this.updateState, this)
    }

    refresh() {
        // if (this.model) {
        //     this.model.off(BaseItemModel.UPDATE_STATE, this.updateState, this)
        // }
        // this.eventProxy.on(BaseItemModel.UPDATE_STATE, this.updateState, this)
        ModuleManager.getLoader().loadRes(this.model.getIcon(), ResType.SpriteFrame, (err, res: ResItem) => {
            if (err || !cc.isValid(this.node)) {
                return;
            }
            this.icon.spriteFrame = res.getRes()
            this.icon.node.scale = this.model.getScale()
        })
        this.updateState();




    }


    onButtonClick() {
        this.controller.onButtonIconClick(this.model)
        // BagManager.addCoin(1, this.node.parent.convertToWorldSpaceAR(this.node.getPosition()))
    }



}
